package com.lanhai.zhonglai.impl;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.lanhai.zhonglai.MsgCore;
import com.lanhai.zhonglai.beans.AgvBean;
import com.lanhai.zhonglai.beans.AgvMountBean;
import com.lanhai.zhonglai.beans.StationBean;
import com.lanhai.zhonglai.beans.StationMountBean;
import com.lanhai.zhonglai.beans.DotBean;
import com.lanhai.zhonglai.dispatch.AgvCmdMap;
import com.lanhai.zhonglai.dispatch.CacheLeaveCheck;
import com.lanhai.zhonglai.dispatch.StationCmdMap;
import com.lanhai.zhonglai.dispatch.StationLeaveCheck;
import com.lanhai.zhonglai.enums.AgvCurrentTransEnum;
import com.lanhai.zhonglai.enums.AgvMountTypeEnum;
import com.lanhai.zhonglai.enums.TransDirectionEnum;
import com.lanhai.zhonglai.interfaces.IAgv;
import com.lanhai.zhonglai.interfaces.IAgvEvent;
import com.lanhai.zhonglai.interfaces.IAgvMount;
import com.lanhai.zhonglai.interfaces.ICmdLog;
import com.lanhai.zhonglai.interfaces.IControlArea;
import com.lanhai.zhonglai.interfaces.IStation;
import com.lanhai.zhonglai.interfaces.IStationMount;
import com.lanhai.zhonglai.interfaces.IStationProtocol;
import com.lanhai.zhonglai.interfaces.IDot;
import com.lanhai.zhonglai.interfaces.IDotEvent;
import com.lanhai.zhonglai.interfaces.IRoad;
import com.lanhai.zhonglai.interfaces.ISendData;
import com.lanhai.zhonglai.third.KSHeartThread;
import com.lanhai.zhonglai.third.StationFactory; 

@Service
public class AgvMount implements IAgvMount {

	@Autowired
	IStation iStation;

	@Autowired
	IStationMount iStationMount;

	@Autowired
	IAgv iAgv;

	@Autowired
	IDot iDot;

	@Autowired
	ISendData sendTools;

	@Autowired
	IAgvEvent iAgvEvent;

	@Autowired
	IDotEvent iDotEvent;

	@Autowired
	IRoad  iRoad ;

	@Autowired
	ICmdLog iCmdLog;
	
	@Autowired
	IControlArea iControlArea;
	
	@Autowired
	AgvCmdMap agvCmdMap;
	
	@Autowired
	StationLeaveCheck stationCheck;
	
    @Autowired
    KSHeartThread heart;
    
    @Autowired
    CacheLeaveCheck cacheCheck;
  
	private static final Logger agvlog = LogManager.getLogger("agvlog");

	
	Gson gson=new Gson();
	
	/**
	 * 双方装货情况的判断
	 */
	@Override
	public void judgement(AgvBean agv, StationBean station, DotBean dot, AgvMountBean agvMount) {
	 
		IStationProtocol stationEvent = StationFactory.getStation(station.getCmdSign());
		if (stationEvent == null) {
			// 机台没有对应的解析 
			MsgCore.sendMsg(agv.getAgvId(), "机台没有对应的解析类");
			return;
		}
		 
	 

		// *********************** 处理离开 ***********************
		// 处理传输完成,满足离开条件就离开
		if (StationCmdMap.canLeave(station.getDotno())) {
			if (station.getCmdSign().equals("ZM") || station.getCmdSign().equals("BM")) {
				if (iControlArea.areaIsLocked(110, agv.getAgvId())) {
					// 前方交管区被管了
					MsgCore.sendMsg(agv.getAgvId(), "交管区锁住中，暂时不能移动");
					
					return;
				}
			} else {
				if (iControlArea.isLockedForDot(dot.getDotno(), agv.getAgvId())) {
					// 前方交管区被管了
					return;
				}
			}
			stationCheck.check(dot, agv, station, stationEvent, agvMount);
			return;
		}

		
		 // ********************* 处理转动 ********************** 
		if (StationCmdMap.canReceive(station.getDotno()) && agv.getCurrentTrans()!=AgvCurrentTransEnum.None) { 
			// 传输过程中，直接过滤
			if(station.getCmdSign().equals("KS")) {
				byte[] cmd = stationEvent.applyRecieve(station.getLine(), station.getFillType());
				//扩散需要添加心跳 
				cmd[3]=heart.getHeart(); 
				sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV发起申请传输", cmd,
						station.getStationname());
			}
			 
			if (agv.isTransing()==false) {
				 agv.setTransing(true);
				 iAgv.saveCache(agv);
				switch (agv.getCurrentTrans()) {
				case DownIn:
					iAgvEvent.transDown(agv, true);
					break;
				case DownOut:
					iAgvEvent.transDown(agv, false);
					break;
				case UpIn:
					iAgvEvent.transUp(agv, true);
					break;
				case UpOut:
					iAgvEvent.transUp(agv, false);
					break;
				case UpInAndDownOut:
					iAgvEvent.transUpInAndDownOut(agv);
					break;
				case UpOutAndDownIn:
					iAgvEvent.transUpOutAndDownIn(agv);
					break;
				default:
					MsgCore.sendMsg(agv.getAgvId(), "未知的传输模式");
					break;
				}
			}
			return;
		}
		 

		// ****************  申请处理传输  **************** 
		// 拿到对接设备的解析方式
		StationMountBean dmount = iStationMount.getBean(station.getDotno());
		if (dmount == null) {
			// 设备装卸信息不存在 
			MsgCore.sendMsg(agv.getAgvId(), "机台装卸信息不存在"); 
			return;
		}
		
		// 机台允许传输，申请皮带转动
		if (StationCmdMap.canTrans(station.getDotno())&&agv.isFinishTrans()==false ) {
			
			if( station.getFillType()!=TransDirectionEnum.Both) {
				MsgCore.sendMsg(agv.getAgvId(), "AGV发起申请传输"); 
				//单层传，简单 
				agv.setBothTrans(false);
				iAgv.saveCache(agv); 
				byte[] cmd = stationEvent.applyRecieve(station.getLine(), station.getFillType());
				//扩散需要添加心跳
				if(station.getCmdSign().equals("KS")) {
					cmd[3]=heart.getHeart();
				} 
				sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV发起申请传输", cmd,
						station.getStationname());
			}else { 
				//双层传，需要判断进和出
				if(dmount.isAllowIn() && dmount.isAllowOut() && agvMount.getMountType()!=AgvMountTypeEnum.AllEmpty ) {
					MsgCore.sendMsg(agv.getAgvId(), "AGV发起申请传输-双层"); 
					agv.setBothTrans(true);
					iAgv.saveCache(agv); 
					byte[] cmd = stationEvent.applyRecieve(station.getLine(), TransDirectionEnum.Both);
					sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV发起申请传输", cmd,
							station.getStationname()); 
				} else if( (agv.isUpHasTrans()==false && agvMount.isUpFull() && dmount.isAllowIn() )
					    	||(agv.isDownHasTrans()==false && agvMount.isDownFull()  && dmount.isAllowIn() ) ) {
					//上层没传输但是上层是满的：上层传出
					//下层没传输但是下层是满的，下层传出
					agv.setBothTrans(false);
					iAgv.saveCache(agv);
					
					MsgCore.sendMsg(agv.getAgvId(), "AGV发起申请传输-传出"); 
					
					byte[] cmd = stationEvent.applyRecieve(station.getLine(), TransDirectionEnum.OnlyIn);
					sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV申请传出", cmd,
							station.getStationname()); 
				}else  if( (agv.isUpHasTrans()==false&&agvMount.isUpFull()==false && dmount.isAllowOut()) 
						||(agv.isDownHasTrans()==false && agvMount.isDownFull()==false  && dmount.isAllowOut() )    ) {
					//上层没传输但是上层是空的：上层传入
					//下层没传输但是下层是空的，下层传入 
					agv.setBothTrans(false);
					iAgv.saveCache(agv);
					
					MsgCore.sendMsg(agv.getAgvId(), "AGV发起申请传输-传入"); 
					
					byte[] cmd = stationEvent.applyRecieve(station.getLine(), TransDirectionEnum.OnlyOut);
					sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV申请传入", cmd,
							station.getStationname()); 
				} else { 
					//单边料不够，当成传输完成处理 
					agv.setFinishTrans(true); 
				}
			}  
			return;
		}

		//  *******************  AGV到位的处理 *************** 
		 
		//修正机台不可传输层的AGV的传输状态
		//注意，这里对双层机台无效
		if (station.isUpTrans() == false) {
			// 上层不可传=上层已经传输完成
			agv.setUpHasTrans(true);
		}
		if (station.isDownTrans() == false) {
			// 下层不可传=下层已经传输完成
			agv.setDownHasTrans(true);
		}
		// ********修正机台不可传输层的AGV的传输状态  end ********
		if (agv.isUpHasTrans() && agv.isDownHasTrans()) {
			agv.isFinishTrans();
		}

		if (agv.isFinishTrans()) {
			// 传输完成后保存日志
			 

			MsgCore.sendMsg(agv.getAgvId(), "AGV申请离开");

			byte[] goout = stationEvent.applyLeave(station.getLine(), station.getFillType());
			// 扩散需要添加心跳
			if (station.getCmdSign().equals("KS")) {
				goout[3] = heart.getHeart();
			}
			sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV申请离开", goout,
					station.getStationname());
			return;
		}
    
		if(station.getCmdSign().equals("POPAID")) {
			if(station.getProcess().equals ("POPAID下料" )) {
				//出料模式
				onlyOut(agv, agvMount, dot, station, stationEvent);
			}else {
				//入料模式
				onlyIn(agv, agvMount, dot, station, stationEvent);
			} 
			return;
		} 
		
		// 只上料部分
		if (station.getFillType() == TransDirectionEnum.OnlyIn) {
			if (dmount.isAllowIn()) {
				agvlog.debug(String.format("AGV【%d】处理上料", agv.getAgvId()));
				onlyIn(agv, agvMount, dot, station, stationEvent);
			} else {
				MsgCore.sendMsg(agv.getAgvId(), String.format("等待花篮数量,进料：%d ",   dmount.getInNum())); 
			} 
			return;
		}

		// 只出料
		if (station.getFillType() == TransDirectionEnum.OnlyOut) {
			if (dmount.isAllowOut()) {
				agvlog.debug(String.format("AGV【%d】处理下料", agv.getAgvId()));
				onlyOut(agv, agvMount, dot, station, stationEvent);
			} else {
				MsgCore.sendMsg(agv.getAgvId(), String.format("等待花篮数量,出料：%d ", dmount.getOutNum())); 
			}
			return;
		} 
	}
 
 

	/**
	 * 机台只上料,机台的角度
	 * 
	 * @param agv
	 * @param agvMount
	 * @param dot
	 * @param station
	 * @param stationEvent
	 */
	void onlyIn(AgvBean agv, AgvMountBean agvMount, DotBean dot, StationBean station, IStationProtocol stationEvent) {
		// 小车上层未操作+小车上面有料+设备上层传输
		if (station.isUpTrans() && agv.isUpHasTrans() == false && agvMount.isUpFull()) {
			// 小车上层发货
			MsgCore.sendMsg(agv.getAgvId(), "AGV已就位,等待机台回复");
			agv.setCurrentTrans(AgvCurrentTransEnum.UpOut);
			agv.setApplyTransType(TransDirectionEnum.OnlyIn);
			iAgv.saveCache(agv);

			// 需要对接设备上层收货
			byte[] cmd = stationEvent.agvArrived(station.getLine(), TransDirectionEnum.OnlyIn);
			//扩散需要添加心跳
			if(station.getCmdSign().equals("KS")) {
				cmd[3]=heart.getHeart();
			}
			sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV就位", cmd,
					station.getStationname());
			return;
		}
		 
		// 小车下层未操作+小车下面有料+设备下层传输
		if (station.isDownTrans() && agv.isDownHasTrans() == false && agvMount.isDownFull()) {
			MsgCore.sendMsg(agv.getAgvId(), "AGV已就位,等待机台回复");
			agv.setCurrentTrans(AgvCurrentTransEnum.DownOut);
			agv.setApplyTransType(TransDirectionEnum.OnlyIn);
			iAgv.saveCache(agv);
			// 需要对接设备下层收货
			byte[] cmd = stationEvent.agvArrived(station.getLine(), TransDirectionEnum.OnlyIn);
			if(station.getCmdSign().equals("KS")) {
				cmd[3]=heart.getHeart();
			}
			sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV就位", cmd,
					station.getStationname());
			return;
		}
		//不可能出现的状态，出现也可能是AGV状态的问题
		MsgCore.sendMsg(agv.getAgvId(), "数量不足，AGV不发起就位信号");
	}

	/**
	 * 机台只下料,机台的角度
	 * 
	 * @param agv
	 * @param agvMount
	 * @param dot
	 * @param station
	 * @param stationEvent
	 */
	void onlyOut(AgvBean agv, AgvMountBean agvMount, DotBean dot, StationBean station, IStationProtocol stationEvent) {
		// 小车上层未操作+小车上面非空+设备上层传输
		if (agv.isUpHasTrans() == false && station.isUpTrans() && agvMount.isUpFull() == false) {
			MsgCore.sendMsg(agv.getAgvId(), "AGV已就位,等待机台回复");
			// 小车上层收货
			agv.setCurrentTrans(AgvCurrentTransEnum.UpIn);
			agv.setApplyTransType(TransDirectionEnum.OnlyOut);
			iAgv.saveCache(agv);

			// 需要对接设备上层发货
			byte[] cmd = stationEvent.agvArrived(station.getLine(), TransDirectionEnum.OnlyOut);
			//扩散需要添加心跳
			if(station.getCmdSign().equals("KS")) {
				cmd[3]=heart.getHeart();
			}
			sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV就位", cmd,
					station.getStationname()); 
			return;
		}
		// 小车下层未操作+小车下面非空 +设备下层传输
		if (agv.isDownHasTrans() == false && station.isDownTrans() && agvMount.isDownFull() == false) {
			MsgCore.sendMsg(agv.getAgvId(), "AGV已就位,等待机台回复");
			// 小车下层收货
			agv.setCurrentTrans(AgvCurrentTransEnum.DownIn);
			agv.setApplyTransType(TransDirectionEnum.OnlyOut);
			iAgv.saveCache(agv);

			// 需要对接设备下层发货
			byte[] cmd = stationEvent.agvArrived(station.getLine(), TransDirectionEnum.OnlyOut);
			//扩散需要添加心跳
			if(station.getCmdSign().equals("KS")) {
				cmd[3]=heart.getHeart();
			}
			sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV就位", cmd,
					station.getStationname());
			return;
		} 
		MsgCore.sendMsg(agv.getAgvId(), "数量不足，AGV不发起就位信号"); 

	}
 
	 
	
	/**
	 * 上下料（发料小车专用，只允许同时传）
	 * 
	 * @param agv
	 * @param agvMount
	 * @param dot
	 * @param station
	 * @param stationEvent
	 */
	void InAndOut_fl(AgvBean agv, AgvMountBean agvMount, DotBean dot, StationBean station, IStationProtocol stationEvent,StationMountBean dmount) {
		//最好的情况，可以同时上下料  
		if (dmount.isAllowIn() && dmount.isAllowOut()&&agv.isDownHasTrans() == false && agv.isUpHasTrans() == false && agvMount.getMountType()!=AgvMountTypeEnum.AllEmpty ) { 
			 MsgCore.sendMsg(agv.getAgvId(), "AGV已就位,等待机台回复");
			if (agvMount.isUpFull()) {
				// 上面有料，上出下入
				agv.setCurrentTrans(AgvCurrentTransEnum.UpOutAndDownIn);
			} else {
				// 上面没料 上入下出
				agv.setCurrentTrans(AgvCurrentTransEnum.UpInAndDownOut);
			}
			agv.setApplyTransType(TransDirectionEnum.Both);
			iAgv.saveCache(agv);
			// 先通知对接设备收货
			byte[] cmd = stationEvent.agvArrived(station.getLine(), TransDirectionEnum.Both);
			sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV就位", cmd,
					station.getStationname());
			return; 
		}  
		//数量不足
		MsgCore.sendMsg(agv.getAgvId(), String.format("等待花篮数量,出料：%d，进料：%d ", dmount.getOutNum(),dmount.getInNum()));
	}
	
	 
	
	// **********************  缓存机的处理 *****************
	
	@Override
	public void judgement_HC(AgvBean agv, StationBean station, DotBean dot, AgvMountBean agvMount) {
		// 装料情况和工序上的判断

		IStationProtocol stationEvent = StationFactory.getStation(station.getCmdSign());
		if (stationEvent == null) {
			// 机台没有对应的解析
			agvlog.debug("机台没有对应的解析类"); 
			return;
		}

		// 处理传输完成,满足离开条件就离开
		if (StationCmdMap.canLeave(station.getDotno())) {
			if(iControlArea.areaIsLocked(0, agv.getAgvId())) {
				//前方交管区被管了
				return;
			} 
			cacheCheck.check(dot, agv, station, stationEvent, agvMount);
			 
			return;
		}

		// 处理转动皮带
		if (StationCmdMap.canReceive(station.getDotno())) {
			// 传输过程中，直接过滤
			if (agv.isTransing()==false) {
				 agv.setTransing(true);
				 iAgv.saveCache(agv);
				switch (agv.getCurrentTrans()) {
				case DownIn:
					iAgvEvent.transDown(agv, true);
					break;
				case DownOut:
					iAgvEvent.transDown(agv, false);
					break;
				case UpIn:
					iAgvEvent.transUp(agv, true);
					break;
				case UpOut:
					iAgvEvent.transUp(agv, false);
					break; 
				default:
					MsgCore.sendMsg(agv.getAgvId(), "未知的传输模式");
					break;
				}
			}
			return;
		}

		// 机台允许传输，申请皮带转动
		/*
		if (StationCmdMap.canTrans(station.getDotno())) {
			if(agvMount.getMountType()==AgvMountTypeEnum.AllEmpty) {
				MsgCore.sendMsg(agv.getAgvId(), "AGV申请传入");
			}else {
				MsgCore.sendMsg(agv.getAgvId(), "AGV申请传出");
			} 
			byte[] cmd = stationEvent.applyRecieve(station.getLine(), station.getFillType());
			sendTools.sendHCData(station.getIp(), station.getPort(), station.getLine(), "AGV申请传输", cmd,
					station.getStationname());
			return;
		}
		 */
		
		if(agv.isUpHasTrans() || agv.isDownHasTrans()||agv.isFinishTrans()) {
			MsgCore.sendMsg(agv.getAgvId(), "传输完成，AGV申请离开");
			if(agv.isFinishTrans()==false) {
				agv.setFinishTrans(true);
				iAgv.saveCache(agv);
			}
			//完成了传输 
			byte[] goout = stationEvent.applyLeave(station.getLine(), TransDirectionEnum.OnlyIn);
			sendTools.sendHCData(station.getIp(), station.getPort(), station.getLine(), "申请离开", goout,
					station.getStationname()); 
		}
		 
		// 拿到对接设备的解析方式
		StationMountBean dmount = iStationMount.getBean(station.getDotno());
		if (dmount == null) {
			// 设备装卸信息不存在
			MsgCore.sendMsg(agv.getAgvId(), "机台装卸信息不存在");
			return;
		}

		if (station.isIsout()) {
			// 出料模式
			if (agvMount.isUpFull() == false && station.isUpTrans()) {
				// AGV上空，机台上层传输
				agv.setCurrentTrans(AgvCurrentTransEnum.UpIn);
				iAgv.saveCache(agv);
				byte[] cmd = stationEvent.applyRecieve(station.getLine(), TransDirectionEnum.OnlyOut);
				sendTools.sendHCData(station.getIp(), station.getPort(), station.getLine(), "申请传输", cmd,
						station.getStationname());
			} else {
				// 错误的状态，不操作
				return;
			}
			if (agvMount.isDownFull() == false && station.isDownTrans()) {
				// AGV下空，机台下层传输
				agv.setCurrentTrans(AgvCurrentTransEnum.DownIn);
				iAgv.saveCache(agv);
				byte[] cmd = stationEvent.applyRecieve(station.getLine(), TransDirectionEnum.OnlyOut);
				sendTools.sendHCData(station.getIp(), station.getPort(), station.getLine(), "申请传输", cmd,
						station.getStationname());
			} else {
				// 错误的状态，不操作
				return;
			}
		} else {
			// 入料模式
			if (agvMount.isUpFull() && station.isUpTrans()) {
				// AGV上满，机台上层传输
				agv.setCurrentTrans(AgvCurrentTransEnum.UpOut);
				iAgv.saveCache(agv);
				byte[] cmd = stationEvent.applyRecieve(station.getLine(), TransDirectionEnum.OnlyIn);
				sendTools.sendHCData(station.getIp(), station.getPort(), station.getLine(), "申请传输", cmd,
						station.getStationname());
			} else {
				// 错误的状态，不操作
				return;
			}
			if (agvMount.isDownFull() && station.isDownTrans()) {
				// AGV下满，机台下层传输
				agv.setCurrentTrans(AgvCurrentTransEnum.DownOut);
				iAgv.saveCache(agv);
				byte[] cmd = stationEvent.applyRecieve(station.getLine(), TransDirectionEnum.OnlyIn);
				sendTools.sendHCData(station.getIp(), station.getPort(), station.getLine(), "申请传输", cmd,
						station.getStationname());
			} else {
				// 错误的状态，不操作
				return;
			}
		}
	}
	
	// *********************** 只允许 上下同时传的 ************
	/**
	 * 判断传输（开始到传输完成，只有同时传，发料小车专用）
	 * @param agv
	 * @param station
	 * @param dot
	 * @param agvMount
	 */
	@Override
	public void judgement_double(AgvBean agv, StationBean station, DotBean dot, AgvMountBean agvMount) {
		// 装料情况和工序上的判断
	  
		IStationProtocol stationEvent = StationFactory.getStation(station.getCmdSign());
		if (stationEvent == null) {
			// 机台没有对应的解析
			agvlog.debug("机台没有对应的解析类");
			MsgCore.sendMsg(agv.getAgvId(), "机台没有对应的解析类");
			return;
		}

		// *********************** 处理离开 ***********************
		// 处理传输完成,满足离开条件就离开
		if (StationCmdMap.canLeave(station.getDotno()) &&
				agvMount.getMountType() == AgvMountTypeEnum.UpFullAndDownEmpty &&agv.isFinishTrans()) {
			if (iControlArea.isLockedForDot(dot.getDotno(), agv.getAgvId())) {
				// 前方交管区被管了
				return;
			} 
			stationCheck.check(dot, agv, station, stationEvent, agvMount);
			return;
		}

		// ********************* 处理转动 **********************
		if (StationCmdMap.canReceive(station.getDotno()) &&agv.isFinishTrans()==false ) { 
			
			if (agv.isTransing()==false) {
				 agv.setTransing(true);
				 iAgv.saveCache(agv);
				if(agvMount.getMountType()==AgvMountTypeEnum.UpEmptyAndDownFull) {
					iAgvEvent.transUpInAndDownOut(agv);
				}else if(agvMount.getMountType()==AgvMountTypeEnum.UpFullAndDownEmpty) {
					iAgvEvent.transUpOutAndDownIn(agv);
				}else {
					MsgCore.sendMsg(agv.getAgvId(), "未知的传输模式");
				}  
			}
			return; 
		}

		// **************** 申请处理传输 ****************
		// 机台允许传输，申请皮带转动
		if (StationCmdMap.canTrans(station.getDotno()) && agv.isFinishTrans() == false) {
			MsgCore.sendMsg(agv.getAgvId(), "申请传输");
			byte[] cmd = stationEvent.applyRecieve(station.getLine(), station.getFillType());

			sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV申请传输", cmd,
					station.getStationname());
			return;
		}

		// ******************* AGV到位的处理 ***************

		// 传输完成后不需要后面的处理，直接申请离开
		if (agv.isFinishTrans()) {
			//传输完成后保存日志 
		 
			byte[] goout = stationEvent.applyLeave(station.getLine(), station.getFillType());
			sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "申请离开", goout,
					station.getStationname());
			return;
		}

		// 拿到对接设备的解析方式
		StationMountBean dmount = iStationMount.getBean(station.getDotno());
		if (dmount == null) {
			// 设备装卸信息不存在
			MsgCore.sendMsg(agv.getAgvId(), "机台装卸信息不存在"); 
			return;
		}  
		if (agv.isUpHasTrans() && agv.isDownHasTrans()) {
			 
			byte[] goout = stationEvent.applyLeave(station.getLine(), station.getFillType()); 
			sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "申请离开", goout,
					station.getStationname());
			return;
		}

		// 同时上下料 
		InAndOut_fl(agv, agvMount, dot, station, stationEvent, dmount);
 
	}
	
	
	 


	@Override
	public void judgement_double_toZR(AgvBean agv, StationBean station, DotBean dot, AgvMountBean agvMount) {
		// 装料情况和工序上的判断
		  
		IStationProtocol stationEvent = StationFactory.getStation(station.getCmdSign());
		if (stationEvent == null) {
			// 机台没有对应的解析
			agvlog.debug("机台没有对应的解析类");
			MsgCore.sendMsg(agv.getAgvId(), "机台没有对应的解析类");
			return;
		}

		// *********************** 处理离开 ***********************
		// 处理传输完成,满足离开条件就离开
		if (StationCmdMap.canLeave(station.getDotno())  && agv.isFinishTrans()) {
			if (iControlArea.isLockedForDot(dot.getDotno(), agv.getAgvId())) {
				// 前方交管区被管了
				return;
			} 
			stationCheck.check(dot, agv, station, stationEvent, agvMount);
			return;
		}

		// ********************* 处理转动 **********************
		if (StationCmdMap.canReceive(station.getDotno()) && agv.getCurrentTrans() != AgvCurrentTransEnum.None) {

			if (agv.isTransing()==false) {
				 agv.setTransing(true);
				 iAgv.saveCache(agv);
				switch (agv.getCurrentTrans()) { 
				case UpInAndDownOut:
					iAgvEvent.transUpInAndDownOut(agv);
					break;
				case UpOutAndDownIn:
					iAgvEvent.transUpOutAndDownIn(agv);
					break;
				default:
					MsgCore.sendMsg(agv.getAgvId(), "未知的传输模式");
					break;
				}
			}
			return;
		}

		// **************** 申请处理传输 ****************
		// 机台允许传输，申请皮带转动
		if (StationCmdMap.canTrans(station.getDotno()) && agv.isFinishTrans() == false) {
			MsgCore.sendMsg(agv.getAgvId(), "申请传输");
			byte[] cmd = stationEvent.applyRecieve(station.getLine(), station.getFillType());

			sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV申请传输", cmd,
					station.getStationname());
			return;
		}

		// ******************* AGV到位的处理 ***************

		// 传输完成后不需要后面的处理，直接申请离开
		if (agv.isFinishTrans()) {
			//传输完成后保存日志 
			 
			byte[] goout = stationEvent.applyLeave(station.getLine(), station.getFillType());
			sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "申请离开", goout,
					station.getStationname());
			return;
		}

		// 拿到对接设备的解析方式
		StationMountBean dmount = iStationMount.getBean(station.getDotno());
		if (dmount == null) {
			// 设备装卸信息不存在
			MsgCore.sendMsg(agv.getAgvId(), "机台装卸信息不存在"); 
			return;
		}  
		if (agv.isUpHasTrans() && agv.isDownHasTrans()) {
			 
			byte[] goout = stationEvent.applyLeave(station.getLine(), station.getFillType()); 
			sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "申请离开", goout,
					station.getStationname());
			return;
		}

		// 同时上下料 
		InAndOut_fl(agv, agvMount, dot, station, stationEvent, dmount);
	}
		
	 

}
